package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.FollowUser;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

/**
 * @author liuyp
 * @date 2021/02/02
 */
@Service
public class FollowUserApiImpl implements FollowUserApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void followUser(long userId, long targetUserId) {
        FollowUser followUser = new FollowUser();
        followUser.setId(ObjectId.get());
        followUser.setUserId(userId);
        followUser.setFollowUserId(targetUserId);
        followUser.setCreated(System.currentTimeMillis());
        mongoTemplate.save(followUser);
    }

    @Override
    public void unfollowUser(long userId, long targetUserId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("followUserId").is(targetUserId);
        mongoTemplate.remove(new Query(criteria), FollowUser.class);
    }
}
